<?php

namespace app\common\model\Order;

use traits\model\SoftDelete;
use think\Model;

class Order extends Model
{
    // TODO 
    protected $table = "pre_order";

    // 自动写入时间
    protected $autoWriteTimestamp = true;

    // 创建时间 => 注册时间
    protected $createTime = 'createtime';

    // 更新时间
    protected $updateTime = false;

    protected $dateFormat = 'Y-m-d';

    // TODO 开启软删除
    use SoftDelete;
    protected $deleteTime = 'deletetime';

    // 追加字段
    protected $append = [
        'createtime_text',
        'status_text', //多张
        'status_class', //多张
        'region_text'
    ];

    protected function getStatusTextAttr($value, $data)
    {
        $status = $data['status'];
        $text = '';

        switch ($status) {
            case 1:
                $text = '已支付，未发货';
                break;
            case 2:
                $text = '已发货，未收货';
                break;
            case 3:
                $text = '已收货，待评价';
                break;
            case 4:
                $text = '已评价';
                break;
            case 5:
                $text = '未支付';
                break;
            case -1:
                $text = '已退货';
                break;
            case -2:
                $text = '退货中';
                break;
            case -3:
                $text = '拒绝退货';
                break;
            default:
                $text = '未知状态';
        }

        return $text;
    }

    protected function getStatusClassAttr($value, $data)
    {
        $status = $data['status'];
        $text = '';

        switch ($status) {
            case 1:
                $text = 'badge badge-success';
                break;
            case 2:
                $text = 'badge badge-info';
                break;
            case 3:
                $text = 'badge badge-cyan';
                break;
            case 4:
                $text = 'badge badge-purple';
                break;
            case 5:
                $text = 'badge badge-warning';
                break;
            case -1:
                $text = 'badge badge-pink';
                break;
            case -2:
                $text = 'badge badge-dark';
                break;
            case -3:
                $text = 'badge badge-danger';
                break;
            default:
                $text = 'badge badge-muted';
        }

        return $text;
    }

    protected function getExpressClassAttr($value, $data)
    {
        $list = [
            '1' => 'badge badge-outline-success',
            '2' => 'badge badge-outline-primary',
            '3' => 'badge badge-outline-info',
            '4' => 'badge badge-outline-warning',
            '5' => 'badge badge-outline-danger',
            '6' => 'badge badge-outline-dark',
            '7' => 'badge badge-outline-brown',
            '8' => 'badge badge-outline-purple',
            '9' => 'badge badge-outline-pink',
            '10' => 'badge badge-outline-cyan',
            '11' => 'badge badge-outline-yellow',
        ];

        return $list[$data['expid']];
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $createtime_text = date('Y-m-d H:i:s', $data['createtime']);
        return $createtime_text;
    }

    // TODO 所在地
    public function getRegionTextAttr($value, $data)
    {
        $address = model('User.Address')->where('id', $data['addrid'])->find();
        $ReogionList = model('Region')->where(['code' => $address['district']])->find();

        $region = $ReogionList['province'] . $ReogionList['city'] . $ReogionList['district'] . $address['address'];

        return $region;
    }


    public function orderpro()
    {
        return $this->belongsTo('app\common\model\Order\OrderProduct', 'id', 'orderid', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User\User', 'userid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function express()
    {
        return $this->belongsTo('app\common\model\Express\Express', 'expid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function address()
    {
        return $this->belongsTo('app\common\model\User\Address', 'addrid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
